package com.aegean.MonitorExamSYS.monitor.action;

import java.io.File;
import java.io.FileInputStream;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Hibernate;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.aegean.MonitorExamSYS.monitor.business.MonitorBusiness;
import com.aegean.MonitorExaxSYS.model.MonitorValue;
import com.aegean.MonitorExaxSYS.model.component.Photo;
import com.opensymphony.xwork2.ActionSupport;
@Controller("MonitorUpdate")
@Scope("prototype")
public class MonitorUpdate extends ActionSupport {

	@Resource
	MonitorBusiness business;
	private MonitorValue monitor;
	private File photo;
	private String photoFileName;
	private String photoContentType;

	public MonitorValue getMonitor() {
		return monitor;
	}

	public void setMonitor(MonitorValue monitor) {
		this.monitor = monitor;
	}

	public File getPhoto() {
		return photo;
	}

	public void setPhoto(File photo) {
		this.photo = photo;
	}

	public String getPhotoFileName() {
		return photoFileName;
	}

	public void setPhotoFileName(String photoFileName) {
		this.photoFileName = photoFileName;
	}

	public String getPhotoContentType() {
		return photoContentType;
	}

	public void setPhotoContentType(String photoContentType) {
		this.photoContentType = photoContentType;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		HttpSession session = ServletActionContext.getRequest().getSession();
		MonitorValue one = (MonitorValue) session.getAttribute("MonitorLogin");
		int Customer_Id = one.getUser_Id();
		if (photo == null || photo.length() == 0) {
			monitor = business.updateMonitor(Customer_Id, monitor, null);
		} else {
			Photo p = new Photo();
			p.setPhoto(Hibernate.createBlob(new FileInputStream(photo)));
			p.setPhoto_Name(photoFileName);
			p.setPhoto_Type(photoContentType);
			monitor = business.updateMonitor(Customer_Id, one, p);
		}
		session.setAttribute("MonitorLogin", monitor);
		return SUCCESS;
	}

}
